package Json;

import com.google.gson.Gson;
import org.json.JSONObject;

import java.io.Serializable;

/**
 * @author Ajie
 * @date 2023/3/9
 * @function
 * 结论：
 * 每一层 JSONObject obj =  new JSONObject(jsonObject.getString("entity"))；
 * 不能执行。 jsonObject.getJSONObject("entity")
 *
 */
public class Json反斜杠_解析 {
    private static final String originStr = "{\"accessRecordId\":\"bcc655ce0dec4da98006d7e796fa1cd2\",\"entity\":\"{\\\"accessRecordId\\\":\\\"bcc655ce0dec4da98006d7e796fa1cd2\\\",\\\"clientsn\\\":\\\"FF78SL22040064\\\",\\\"name\\\":\\\"柯锦龙\\\",\\\"obj\\\":\\\"{\\\\\\\"avatars\\\\\\\":{\\\\\\\"NEAR_INFRARED\\\\\\\":[\\\\\\\"file://柯锦龙_20230309111437764_nir.jpeg\\\\\\\"],\\\\\\\"VISIBLE_LIGHT\\\\\\\":[\\\\\\\"file://柯锦龙_20230309111437764.jpeg\\\\\\\"]},\\\\\\\"deviceInfo\\\\\\\":{\\\\\\\"name\\\\\\\":\\\\\\\"239slave我是从设备\\\\\\\",\\\\\\\"sn\\\\\\\":\\\\\\\"FF78SL22040064\\\\\\\"},\\\\\\\"extension\\\\\\\":{},\\\\\\\"faceResultType\\\\\\\":\\\\\\\"faceCheckSuccess_noBodyTemp_right\\\\\\\",\\\\\\\"id\\\\\\\":\\\\\\\"bcc655ce0dec4da98006d7e796fa1cd2\\\\\\\",\\\\\\\"passMode\\\\\\\":\\\\\\\"FACE\\\\\\\",\\\\\\\"personInfo\\\\\\\":{\\\\\\\"groupId\\\\\\\":\\\\\\\"normal\\\\\\\",\\\\\\\"icNumber\\\\\\\":\\\\\\\"\\\\\\\",\\\\\\\"name\\\\\\\":\\\\\\\"柯锦龙\\\\\\\",\\\\\\\"no\\\\\\\":\\\\\\\"12345666\\\\\\\",\\\\\\\"organizationId\\\\\\\":0,\\\\\\\"organizationIdsNew\\\\\\\":[\\\\\\\"1\\\\\\\"],\\\\\\\"organizationNames\\\\\\\":[\\\\\\\"FaceSec\\\\\\\"],\\\\\\\"ruleId\\\\\\\":\\\\\\\"999999999\\\\\\\",\\\\\\\"tenantId\\\\\\\":0,\\\\\\\"verificationPassword\\\\\\\":\\\\\\\"\\\\\\\",\\\\\\\"version\\\\\\\":0,\\\\\\\"wgNumber\\\\\\\":\\\\\\\"\\\\\\\"},\\\\\\\"resultType\\\\\\\":\\\\\\\"TERMINAL_VALIDATION_PASS\\\\\\\",\\\\\\\"score\\\\\\\":\\\\\\\"83.74022\\\\\\\",\\\\\\\"syncTime\\\\\\\":1678331677764}\\\",\\\"organizationIdsNew\\\":\\\"1,\\\",\\\"organizationNames\\\":\\\"FaceSec,\\\",\\\"passmode\\\":0,\\\"passmodeDetail\\\":-1,\\\"recordUploadflag\\\":0,\\\"timeinserted\\\":1678331678085,\\\"timereceived\\\":1678331677764,\\\"uuid\\\":\\\"12345666\\\"}\",\"livePhoto\":\"\"}";


    public static void main(String[] args) throws Exception{

//        反斜杠
        JSONObject jsonObject = new JSONObject(originStr);
//        JSONObject entityObj = jsonObject.getJSONObject("entity");//不能解析 Exception :: JSONObject["entity"] is not a JSONObject

        JSONObject entity = new JSONObject(jsonObject.getString("entity"));
        String accessRecordId = entity.getString("accessRecordId");//可以解析。
        System.out.println("Json反斜杠_解析:main:"+accessRecordId);




        //正常 jsonObject.
        String rightObjField = "{\n" +
                "    \"accessRecordId\":\"bcc655ce0dec4da98006d7e796fa1cd2\",\n" +
                "    \"entity\":{\n" +
                "        \"accessRecordId\":\"bcc655ce0dec4da98006d7e796fa1cd2\",\n" +
                "        \"clientsn\":\"FF78SL22040064\"\n" +
                "    },\n" +
                "    \"livePhoto\":\"\"\n" +
                "}";

        System.out.println("Json反斜杠_解析:main:GSON: 正常 JsonObject:"+new Gson().fromJson(rightObjField, Data.class).entity.accessRecordId);
        System.out.println("Json反斜杠_解析:main:GSON: 正常 JsonObject,测试不嵌套类的结构:"+new Gson().fromJson(rightObjField, Data.class).entity.accessRecordId);
        System.out.println("Json反斜杠_解析:main:GSON: 正常 JsonObject:最外层不是内部类:"+new Gson().fromJson(rightObjField, Json反斜杠_解析_DataBean.class).getEntity().getAccessRecordId());

        //异常 jonsObject 变 String。 加""
        String errorObjFieldStr = "{\n" +
                "    \"accessRecordId\":\"bcc655ce0dec4da98006d7e796fa1cd2\",\n" +
                "    \"entity\":\"{\"accessRecordId\":\"bcc655ce0dec4da98006d7e796fa1cd2\",\"clientsn\":\"FF78SL22040064\"}\",\n" +
                "    \"livePhoto\":\"\"\n" +
                "}";
//        Data data = new Gson().fromJson(originStr, Data.class);//java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 64 path $.entity





    }


     static class Data implements Serializable {
         /**
          * accessRecordId : bcc655ce0dec4da98006d7e796fa1cd2
          * entity : {"accessRecordId":"bcc655ce0dec4da98006d7e796fa1cd2","clientsn":"FF78SL22040064"}
          * livePhoto :
          */
         private String accessRecordId;
         private EntityBean2 entity;
         private String livePhoto;

         public static class EntityBean implements Serializable {
             /**
              * accessRecordId : bcc655ce0dec4da98006d7e796fa1cd2
              * clientsn : FF78SL22040064
              */
             private String accessRecordId;
             private String clientsn;
         }
     }

    static class EntityBean2 implements Serializable {
        /**
         * accessRecordId : bcc655ce0dec4da98006d7e796fa1cd2
         * clientsn : FF78SL22040064
         */
        private String accessRecordId;
        private String clientsn;
    }


}
